<?php

namespace App\Common\Util\ImitateAop\Aspects;

use Sc\Util\ImitateAopProxy\Interfaces\ImitateAspectInterface;
use think\facade\Db;

/**
 * 切入事务
 *
 * Class Transaction
 */
class TransactionAspect implements ImitateAspectInterface
{
    /**
     * @param \Closure $closure
     * @param array    $args
     *
     * @return mixed
     */
    public function handle(\Closure $closure, array $args): mixed
    {
        Db::startTrans();
        try {

            $result = $closure($args);

        } catch (\Throwable $exception) {
            Db::rollBack();
            throw $exception;
        }
        Db::commit();

        return $result;
    }
}
